package com.zhiketuan.webserver.swagger;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.async.DeferredResult;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * Swagger 配置
 *
 * @author 凡梦星尘(elkan1788@gmail.com)
 */
@Configuration
@EnableSwagger2
public class ApplicationSwaggerConfig {

    @Value("#{setting['swagger_title']}")
    private String title;
    @Value("#{setting['swagger_description']}")
    private String description;
    @Value("#{setting['swagger_version']}")
    private String version;
    @Value("#{setting['swagger_terms_service_url']}")
    private String termsOfServiceUrl;
    @Value("#{setting['swagger_contact']}")
    private String contact;
    @Value("#{setting['swagger_license']}")
    private String license;
    @Value("#{setting['swagger_license_url']}")
    private String licenseUrl;


    @Bean
    public Docket apiInfoDocket() {
        Docket docket = new Docket(DocumentationType.SWAGGER_2);
        ApiInfo apiInfo = new ApiInfo(
                title,
                description,
                version,
                termsOfServiceUrl,
                contact,
                license,
                licenseUrl);
        docket.apiInfo(apiInfo);

        List<Parameter> parameters = new ArrayList<>();
        Parameter authParameter = new ParameterBuilder().name("Authorization").description("授权码")
                .modelRef(new ModelRef("string")).parameterType("header").defaultValue("ZhiKeTuan ")
                .required(true).build();
        parameters.add(authParameter);
        docket.globalOperationParameters(parameters);

        docket.genericModelSubstitutes(DeferredResult.class).useDefaultResponseMessages(true)
                .forCodeGeneration(false).select().apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.regex("/api/.*")).build();

        return docket;
    }
}